﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для wStation.xaml
    /// </summary>
    public partial class wStation : Window
    {
        private routelistEntities routelistEntities;

        public wStation()
        {
            InitializeComponent();
            routelistEntities = new routelistEntities();
        }

        private System.Data.Objects.ObjectQuery<Station> GetStationQuery(routelistEntities routelistEntities)
        {
            // Автоматически созданный код

            System.Data.Objects.ObjectQuery<RL.Station> stationQuery = routelistEntities.Station;
            // Возвращает ObjectQuery.
            return stationQuery;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            //RL.routelistEntities routelistEntities = new RL.routelistEntities();
            // Загрузить данные в Station. Можно изменить этот код как требуется.
            System.Windows.Data.CollectionViewSource stationViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("stationViewSource")));
            System.Data.Objects.ObjectQuery<RL.Station> stationQuery = this.GetStationQuery(routelistEntities);
            stationViewSource.Source = stationQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            SaveData();
        }

        private void buttonAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Windows.Data.CollectionViewSource stationViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("stationViewSource")));
                IEditableCollectionView cv = (IEditableCollectionView)stationViewSource.View;
                Station st = (Station)cv.AddNew();
                cv.CommitNew();
                stationViewSource.View.MoveCurrentTo(cv);
                stationDataGrid.ScrollIntoView(cv);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Windows.Data.CollectionViewSource stationViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("stationViewSource")));
                Station st = (Station)stationViewSource.View.CurrentItem;
                if (null == st) return;
                routelistEntities.DeleteObject(st);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private bool SaveData()
        {
            bool bResult = true;
            try
            {
                routelistEntities.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                bResult = false;
            }
            return bResult;
        }


        private bool IsModified()
        {
            bool bResult = false;
            if (null == routelistEntities) return false;
            bResult = routelistEntities.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added).Count() > 0 || routelistEntities.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Deleted).Count() > 0 || routelistEntities.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified).Count() > 0;
            return bResult;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (IsModified())
            {
                var result = MessageBox.Show("Сохранить изменения?", "Предупреждение", MessageBoxButton.YesNoCancel, MessageBoxImage.Exclamation, MessageBoxResult.Yes);
                switch (result)
                {
                    case MessageBoxResult.Yes:
                        if (false == SaveData())
                        {
                            e.Cancel = true;
                        }
                        break;
                    case MessageBoxResult.Cancel:
                        e.Cancel = true;
                        break;
                }

            }
        }
    }
}
